# Building
TARGET := riscv64gc-unknown-none-elf
MODE := release
KERNEL_ELF := ./os/target/$(TARGET)/$(MODE)/os
# KERNEL_BIN := $(KERNEL_ELF).bin
# os与os.bin是什么关系
KERNEL_BIN := $(KERNEL_ELF)
DISASM_TMP := target/$(TARGET)/$(MODE)/asm
# FS_IMG := ./user/target/$(TARGET)/$(MODE)/fs.img
# FS_IMG := ./user/target/$(TARGET)/$(MODE)/uImage
# FS_IMG := ./testfile
FS_IMG := ./disk.img.ori
APPS := ../user/src/bin/*

# KERNEL ENTRY
KERNEL_ENTRY_PA := 0x80200000

# BOARD
BOARD := qemu
SBI ?= rustsbi
BOOTLOADER := ./bootloader/$(SBI)-$(BOARD).bin

QEMU_ARGS := -machine virt \
			 -nographic \
			 -bios $(BOOTLOADER) \
			 -device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA) \
			 -drive file=$(FS_IMG),if=none,format=raw,id=x0 \
			 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

# run:
# 	@echo "build kernel"
# 	@pushd.\os\; cargo build --release; popd; Get-ChildItem -Path.\os\ | Select-Object -ExpandProperty Name
# 	@qemu-system-riscv64 $(QEMU_ARGS)
run:
	@echo "build user apps"
	@cd ./user && make build
	@echo "build kernel"
	@cd ./os && cargo build --release
	@qemu-system-riscv64 $(QEMU_ARGS)

# all:
# 	@mkdir ./os/.cargo && cp -r ./os/cargo/* ./os/.cargo/
# 	@mkdir ./user/.cargo && cp -r ./user/cargo/* ./user/.cargo/
# 	@echo "build user apps"
# 	@cd ./user && make build
# 	@echo "build kernel"
# 	@cd ./os && cargo build --release
# 	@\cp $(KERNEL_BIN) ./kernel-qemu && \cp $(BOOTLOADER) ./sbi-qemu
# 	@echo "success"

all:
	@echo "start"


#@cp $(KERNEL_BIN) kernel-qemu
#@cp $(BOOTLOADER) sbi-qemu
	

test:
	@qemu-system-riscv64 -machine virt \
			 -nographic \
			 -bios sbi-qemu \
			 -device loader,file=kernel-qemu,addr=$(KERNEL_ENTRY_PA) \
			 -drive file=$(FS_IMG),if=none,format=raw,id=x0 \
			 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0


# @qemu-system-riscv64 -machine virt \
	# 		 -nographic \
	# 		 -bios sbi-qemu \
	# 		 -device loader,file=kernel-qemu,addr=$(KERNEL_ENTRY_PA) \
	# 		 -drive file=$(FS_IMG),if=none,format=raw,id=x0 \
	# 		 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0


# GDB := ../riscv64-unknown-elf-gdb.exe

debug1:
	@echo "build kernel"
	@cd ./os && cargo build --release
	@echo "build user apps"
	@cd ./user && make build
	@qemu-system-riscv64 $(QEMU_ARGS) -s -S
	# @tmux new-session -d \
	# 	"qemu-system-riscv64 $(QEMU_ARGS) -s -S" && \
	# 	tmux split-window -h "../riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \
	# 	tmux -2 attach-session -d

debug2:
	@../riscv64-unknown-elf-gdb.exe -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'

# debug:
# 	@echo "build kernel"
# 	@cd ./os && cargo build --release
# 	@echo "build user apps"
# 	@cd ./user && make build
# 	@tmux new-session -d \
# 		"qemu-system-riscv64 $(QEMU_ARGS) -s -S" && \
# 		tmux split-window -h "../riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \
# 		tmux -2 attach-session -d
